<?php

namespace App\Service\OpenShop\SiteLocation;

use App\Enums\OpenShop\InvestmentPromotion\SiteLocationEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Models\SiteLocation\CityShopFirstOrderPriceModel;
use App\Remote\Rms\RmsAreaRemote;
use App\Service\BaseService;
use App\Service\SiteLocation\Import\CityShopFirstOrderPrice;
use App\Service\WaiMai\WaiMaiAnalysisService;
use GuzzleHttp\Exception\GuzzleException;

class CapitalBudgetingService extends BaseService
{
    public OpenShopSiteLocationModel  $siteLocationModel;
    public ?OpenShopUserContractModel $contract     = null;
    public int                        $annualPaymentShopCycle;
    public int                        $annualPaymentWarehouseCycle;
    public float                      $floatingRate = 2 / 12;

    /**
     * init
     */
    public function __construct($model)
    {
        $this->siteLocationModel = $model;

        if ($model->source == OpenShopSiteLocationModel::SOURCE_OPEN_SHOP) {
            $contractModel = OpenShopUserContractModel::query()
                ->where('orderNo', $this->siteLocationModel->orderNo)
                ->where('contractStatus', OpenShopUserContractModel::CONTRACT_STATUS_NORMAL)
                ->first();

            $this->contract = $contractModel;
        }

        // 获得商铺年支付周期
        switch ($this->siteLocationModel->rentPaymentCycle) {
            case SiteLocationEnum::RENT_PAYMENT_CYCLE_1: // 月付
                $this->annualPaymentShopCycle = 12;
                break;
            case SiteLocationEnum::RENT_PAYMENT_CYCLE_2: // 季付
                $this->annualPaymentShopCycle = 4;
                break;
            case SiteLocationEnum::RENT_PAYMENT_CYCLE_3: // 半年付
                $this->annualPaymentShopCycle = 2;
                break;
            case SiteLocationEnum::RENT_PAYMENT_CYCLE_4: // 年付
                $this->annualPaymentShopCycle = 1;
                break;
            default:
                $this->annualPaymentShopCycle = 12;
                break;
        }

        // 获得库房年支付周期
        switch ($this->siteLocationModel->warehouseRentPaymentCycle) {
            case SiteLocationEnum::RENT_PAYMENT_CYCLE_1: // 月付
                $this->annualPaymentWarehouseCycle = 12;
                break;
            case SiteLocationEnum::RENT_PAYMENT_CYCLE_2: // 季付
                $this->annualPaymentWarehouseCycle = 4;
                break;
            case SiteLocationEnum::RENT_PAYMENT_CYCLE_3: // 半年付
                $this->annualPaymentWarehouseCycle = 2;
                break;
            case SiteLocationEnum::RENT_PAYMENT_CYCLE_4: // 年付
                $this->annualPaymentWarehouseCycle = 1;
                break;
            default:
                $this->annualPaymentWarehouseCycle = 12;
                break;
        }
    }


    public function json(): array
    {
        return [
            $this->fixedCostsBeforeOpening(),          // 开业前固定成本
            $this->fixedOperatingCostsMonthly(),       // 月经营固定成本
            $this->changeOperatingCostsMonthly(),      // 月经营变动成本
            $this->reliefItems(),                      // 减免项目
            $this->recoverableCosts(),                 // 可回收成本
        ];
    }

    public function table(): array
    {
        return [
            $this->monthlyRevenueEstimate(), // 营收预估
            $this->estimatedPaybackPeriod(), // 回本周期预估
        ];
    }

    /**
     * 首批订货金额
     */
    public function firstBatchOrderPrice(): int
    {
        $price = 20000;
        if ($this->siteLocationModel->cityCode) {
            $price = CityShopFirstOrderPriceModel::getPrice($this->siteLocationModel->cityCode);
        }
        return $price;
    }

    /**
     * 装修成本
     * @return int
     */
    public function decorationCost(): int
    {
        switch ($this->siteLocationModel->rentType) {
            case SiteLocationEnum::RENT_TYPE_2:
                $rent = bcmul($this->actualMonthlyIncome(), (((int)$this->siteLocationModel->remind) / 100)); // 提点
                break;
            default:
                $rent = ((int)$this->siteLocationModel->fixedRent) / 12 / 100;
        }

        $price =  47012.36
            + (132.83 * (int)$this->siteLocationModel->area)
            + $rent * 0.75
            + (SiteLocationEnum::CITY_TYPE_LEVEL_DECORATION_COST[(int)$this->siteLocationModel->cityType] ?? 0)
            + (in_array($this->siteLocationModel->shopModelType, [SiteLocationEnum::SHOP_MODEL_TYPE_MALL, SiteLocationEnum::SHOP_MODEL_TYPE_MALL_INSIDE, SiteLocationEnum::SHOP_MODEL_TYPE_MALL_STREET]) ? +2884.61 : -2884.61);

        return (int)$price;
    }

    /**
     * 月实收（堂食+外卖）
     */
    public function actualMonthlyIncome(): int
    {
        // 门店类型
        switch ($this->siteLocationModel->shopModelType) {
            case SiteLocationEnum::SHOP_MODEL_TYPE_MALL:
            case SiteLocationEnum::SHOP_MODEL_TYPE_MALL_STREET:
            case SiteLocationEnum::SHOP_MODEL_TYPE_COMMUNITY_STREET:
            case SiteLocationEnum::SHOP_MODEL_TYPE_COMMUNITY_SCHOOL:
                $a = 2;
                break;
            default:
                $a = 1;
                break;
        }

        // 面积  城市等级
        $area = is_numeric($this->siteLocationModel->area) ? $this->siteLocationModel->area : 0;
        $b    = SiteLocationEnum::CITY_TYPE_LEVEL_COEFFICIENT[$this->siteLocationModel->cityType] ?? 6;

        // 月实收
        $monthlyIncome = (int)(50576 + 8447 * $a - 2267.6 * $b + 75.3 * $area);
        $warehouseRent = bcdiv((int)$this->siteLocationModel->warehouseFixedRent, 12);
        switch ($this->siteLocationModel->rentType) {
            case SiteLocationEnum::RENT_TYPE_2:
                //$tempRent      = bcmul($monthlyIncome, (((int)$this->siteLocationModel->remind) / 100)); // 提点
                $monthlyIncome = $monthlyIncome + 2.355 * $warehouseRent;
                break;
            default:
                $shopRent      = bcdiv($this->siteLocationModel->fixedRent, 12) / 100;
                $monthlyIncome = $monthlyIncome + 2.355 * ($shopRent + $warehouseRent);
        }

        return (int)$monthlyIncome;
    }

    /**
     * 加盟费
     */
    public function franchiseFee(): int
    {
        // 加盟费
        $franchiseFee = 49800;
        if ($this->siteLocationModel->source == OpenShopSiteLocationModel::SOURCE_OPEN_SHOP) {
            if ($this->contract != null) {
                $franchiseFee = ($this->contract->guidanceFee + $this->contract->technologyTransferFee + $this->contract->initialFee) / $this->contract->shopCount / 100;
            }
        }
        return (int)$franchiseFee;
    }

    /**
     * 咨询指导费
     */
    public function guidanceFee(): int
    {
        // 咨询指导费
        $guidanceFee = 19800;
        if ($this->siteLocationModel->source == OpenShopSiteLocationModel::SOURCE_OPEN_SHOP) {
            if ($this->contract != null) {
                $guidanceFee = $this->contract->guidanceFee / $this->contract->shopCount / 100;
            }
        }
        return (int)$guidanceFee;
    }

    /**
     * 品牌使用费
     */
    public function initialFee(): int
    {
        // 品牌使用费
        $initialFee = 15000;
        if ($this->siteLocationModel->source == OpenShopSiteLocationModel::SOURCE_OPEN_SHOP) {
            if ($this->contract != null) {
                $initialFee = $this->contract->initialFee / $this->contract->shopCount / 100;
            }
        }
        return (int)$initialFee;
    }

    /**
     * 技术转让费
     */
    public function technologyTransferFee(): int
    {
        // 技术转让费
        $technologyTransferFee = 15000;
        if ($this->siteLocationModel->source == OpenShopSiteLocationModel::SOURCE_OPEN_SHOP) {
            if ($this->contract != null) {
                $technologyTransferFee = $this->contract->technologyTransferFee / $this->contract->shopCount / 100;
            }
        }
        return (int)$technologyTransferFee;
    }

    /**
     * 管理费
     */
    public function managementFee(): int
    {
        $managementFee = 12000;
        if ($this->contract != null) {
            $managementFee = $this->contract->managementCost / $this->contract->shopCount / 100;
        }
        return $managementFee;
    }

    /**
     * 合同保证金
     */
    public function contractDeposit(): int
    {
        $contractDeposit = 10000;
        if ($this->contract != null) {
            $contractDeposit = $this->contract->deposit / $this->contract->shopCount / 100;
        }
        return $contractDeposit;
    }

    /**
     * 开业前固定成本
     */
    public function fixedCostsBeforeOpening(): array
    {
        switch ($this->siteLocationModel->rentType) {
            case SiteLocationEnum::RENT_TYPE_2:
                $initialShopRent = 0;
                break;
            default:
                $initialShopRent = (int)ceil((int)$this->siteLocationModel->fixedRent / 100 / $this->annualPaymentShopCycle);
        }

        $initialWarehouseRent = (int)ceil((int)$this->siteLocationModel->warehouseFixedRent / $this->annualPaymentWarehouseCycle);

        $detail = [
            [
                'text'   => '加盟费',
                'price'  => $this->franchiseFee(),
                'value'  => $this->franchiseFee() . ' 元',
                'detail' => [
                    [
                        'text'  => '咨询服务费', // guidanceFee
                        'value' => $this->guidanceFee() . ' 元',
                    ], [
                        'text'  => '品牌使用费', // initialFee
                        'value' => $this->initialFee() . ' 元',
                    ], [
                        'text'  => '技术转让费', // technologyTransferFee
                        'value' => $this->technologyTransferFee() . ' 元',
                    ]
                ]
            ], [
                'text'  => '管理费',
                'price' => $this->managementFee(),
                'value' => $this->managementFee() . ' 元/年',
            ], [
                'text'   => '营建费用',
                'price'  => 60000 + $this->decorationCost(),
                'value'  => (60000 + $this->decorationCost()) . ' 元',
                'detail' => [
                    [
                        'text'  => '设计费',
                        'value' => '6000 元',
                    ], [
                        'text'  => '软装',
                        'value' => '15000 元',
                    ], [
                        'text'  => '硬装',
                        'value' => $this->decorationCost() . ' 元',
                    ], [
                        'text'  => '操作设备（炸炉、展柜设备等）',
                        'value' => '18000 元',
                    ], [
                        'text'  => '电子设备（商屏、收银等）',
                        'value' => '20000 元',
                    ]
                ]
            ], [
                'text'   => '房租杂费',
                'price'  => $initialShopRent + $initialWarehouseRent + (int)$this->siteLocationModel->propertyFee + (int)$this->siteLocationModel->manageFee + (int)$this->siteLocationModel->sponsorshipFee + (int)$this->siteLocationModel->promotionFee + (int)$this->siteLocationModel->otherFee,
                'value'  => ($initialShopRent + $initialWarehouseRent + (int)$this->siteLocationModel->propertyFee + (int)$this->siteLocationModel->manageFee + (int)$this->siteLocationModel->sponsorshipFee + (int)$this->siteLocationModel->promotionFee + (int)$this->siteLocationModel->otherFee) . ' 元',
                'detail' => [
                    [
                        'text'  => '初期商铺房租',
                        'value' => $initialShopRent . ' 元',
                    ], [
                        'text'  => '初期仓库房租',
                        'value' => $initialWarehouseRent . ' 元',
                    ], [
                        'text'  => '物业费',
                        'value' => (int)$this->siteLocationModel->propertyFee . ' 元',
                    ], [
                        'text'  => '其他杂费（管理费、赞助费、推广费等）',
                        'value' => (int)$this->siteLocationModel->manageFee + (int)$this->siteLocationModel->sponsorshipFee + (int)$this->siteLocationModel->promotionFee + (int)$this->siteLocationModel->otherFee . ' 元',
                    ]
                ]
            ], [
                'text'  => '进场费',
                'price' => (int)$this->siteLocationModel->mobilizationFee,
                'value' => (int)$this->siteLocationModel->mobilizationFee . ' 元',
            ], [
                'text'  => '商户通',
                'price' => 1500,
                'value' => '1500 元',
            ], [
                'text'   => '自采费用',
                'price'  => 13000,
                'value'  => '13000 元',
                'detail' => [
                    [
                        'text'  => '自采设备',
                        'value' => '10000 元',
                    ], [
                        'text'  => '置物杂物费',
                        'value' => '1500 元',
                    ], [
                        'text'  => '其他器具等',
                        'value' => '1500 元',
                    ]
                ]
            ], [
                'text'   => '筹备费用',
                'price'  => $this->firstBatchOrderPrice() + 16000,
                'value'  => $this->firstBatchOrderPrice() + 16000 . ' 元',
                'detail' => [
                    [
                        'text'  => '首批订货',
                        'value' => $this->firstBatchOrderPrice() . ' 元',
                    ], [
                        'text'  => '开业营销物料',
                        'value' => '1500 元',
                    ], [
                        'text'  => '开业食材储备',
                        'value' => '6500 元',
                    ], [
                        'text'  => '培训差旅费',
                        'value' => '3000 元',
                    ], [
                        'text'  => '前置人工费',
                        'value' => '2000 元',
                    ], [
                        'text'  => '日用杂费（含水电、围挡）',
                        'value' => '3000 元',
                    ]
                ]
            ]
        ];

        return [
            'text'   => '开业前固定成本',
            'value'  => (int)collect($detail)->sum('price') . ' 元',
            'detail' => $detail
        ];
    }

    /**
     * 月经营固定成本
     */
    public function fixedOperatingCostsMonthly(): array
    {
        switch ($this->siteLocationModel->rentType) {
            case SiteLocationEnum::RENT_TYPE_2:
                $initialShopRent = bcmul($this->actualMonthlyIncome(), ((int)$this->siteLocationModel->remind / 100));
                break;
            default:
                $initialShopRent = bcdiv((int)$this->siteLocationModel->fixedRent, 1200);
        }

        $initialWarehouseRent = (int)ceil((int)$this->siteLocationModel->warehouseFixedRent / 12);

        return [
            'text'   => '月经营固定成本',
            'price'  => $initialShopRent + $initialWarehouseRent,
            'value'  => ($initialShopRent + $initialWarehouseRent) . ' 元',
            'detail' => [
                [
                    'text'   => '房租',
                    'value'  => ($initialShopRent + $initialWarehouseRent) . ' 元',
                    'detail' => [
                        [
                            'text'  => '商铺租金',
                            'value' => $initialShopRent . ' 元',
                        ], [
                            'text'  => '仓库租金',
                            'value' => $initialWarehouseRent . ' 元',
                        ]
                    ]
                ]
            ]
        ];
    }

    /**
     * 月经营变动成本
     */
    public function changeOperatingCostsMonthly(): array
    {
        // 月实收
        $realIncome = $this->actualMonthlyIncome();

        $order         = 40;
        $operatingCost = 4;
        $total         = 64;
        if ($this->siteLocationModel->remind >= 25 && $this->siteLocationModel->rentType != SiteLocationEnum::RENT_TYPE_1) {
            $order         = 20;
            $operatingCost = 2;
            $total         = 42;
        }

        return [
            'text'   => '月经营变动成本',
            'price'  => (int)ceil($realIncome * ($total / 100)),
            'value'  => (int)ceil($realIncome * ($total / 100)) . ' 元',
            'detail' => [
                [
                    'text'  => '人力成本(实收15%)',
                    'value' => (int)ceil($realIncome * 0.15) . ' 元',
                ], [
                    'text'  => '能源水电(实收4%)',
                    'value' => (int)ceil($realIncome * 0.04) . ' 元',
                ], [
                    'text'  => '食材订货(实收' . $order . '%)',
                    'price' => (int)ceil($realIncome * ($order / 100)),
                    'value' => (int)ceil($realIncome * ($order / 100)) . ' 元',
                ], [
                    'text'  => '运营成本(实收' . $operatingCost . '%)',
                    'value' => (int)ceil($realIncome * ($operatingCost / 100)) . ' 元',
                ], [
                    'text'  => '日常杂费(实收1%)',
                    'value' => (int)ceil($realIncome * 0.01) . ' 元',
                ]
            ]
        ];
    }

    /**
     * 减免项目
     */
    public function reliefItems(): array
    {
        $operateSubsidyAmount = (int)optional($this->contract)->operateSubsidyAmount / 100;
        $kindSubsidyAmount    = (int)optional($this->contract)->kindSubsidyAmount / 100;

        return [
            'text'   => '减免项目',
            'price'  => $operateSubsidyAmount + $kindSubsidyAmount,
            'value'  => ($operateSubsidyAmount + $kindSubsidyAmount) . ' 元',
            'detail' => [
                [
                    'text'  => '线上补贴',
                    'value' => $operateSubsidyAmount . ' 元',
                ], [
                    'text'  => '订货补贴',
                    'value' => $kindSubsidyAmount . ' 元',
                ]
            ]
        ];
    }

    /**
     * 可回收成本
     */
    public function recoverableCosts(): array
    {
        // 可回收成本 = 商铺押金 + 库房押金 + 装修押金 + 保证金 + 转让费
        $recoverableCosts = ((int)$this->siteLocationModel->deposit + (int)$this->siteLocationModel->warehouseDeposit + $this->contractDeposit() + (int)$this->siteLocationModel->bond + (int)$this->siteLocationModel->transferFee + ((int)optional($this->contract)->decorationDeposit / 100));

        return [
            'text'   => '可回收成本',
            'price'  => $recoverableCosts,
            'value'  => $recoverableCosts . ' 元',
            'detail' => [
                [
                    'text'   => '押金',
                    'value'  => ((int)$this->siteLocationModel->deposit + (int)$this->siteLocationModel->warehouseDeposit + ((int)optional($this->contract)->decorationDeposit / 100)) . ' 元',
                    'detail' => [
                        [
                            'text'  => '商铺房租押金',
                            'value' => (int)$this->siteLocationModel->deposit . ' 元',
                        ], [
                            'text'  => '仓库房租押金',
                            'value' => (int)$this->siteLocationModel->warehouseDeposit . ' 元',
                        ], [
                            'text'  => '装修押金',
                            'value' => ((int)optional($this->contract)->decorationDeposit / 100) . ' 元',
                        ]
                    ]
                ], [
                    'text'   => '保证金',
                    'value'  => ($this->contractDeposit() + (int)$this->siteLocationModel->bond) . ' 元',
                    'detail' => [
                        [
                            'text'  => '商铺保证金',
                            'value' => (int)$this->siteLocationModel->bond . ' 元',
                        ], [
                            'text'  => '加盟保证金',
                            'value' => $this->contractDeposit() . ' 元',
                        ]
                    ]
                ], [
                    'text'  => '转让费',
                    'value' => (int)$this->siteLocationModel->transferFee . ' 元',
                ]
            ]
        ];
    }

    /**
     * 外卖预估
     * @return int|mixed
     * @throws \Exception
     */
    public function waimaiEstimateMonth()
    {
        $estimate = 0;
        if ($this->siteLocationModel->longitude && $this->siteLocationModel->latitude) {
            $takeOutData = (new WaiMaiAnalysisService())
                ->setLngLat($this->siteLocationModel->longitude, $this->siteLocationModel->latitude);
            $estimate    = array_sum($takeOutData->calcEstimateData()['estimate'] ?? []);
        }
        return $estimate * 30;
    }

    /**
     * 月营收预估
     */
    public function monthlyRevenueEstimate(): array
    {
        $actualMonthlyIncomeValue      = $this->actualMonthlyIncome();
        $dineInValue                   = $this->actualMonthlyIncome() - $this->waimaiEstimateMonth();
        $waimaiEstimateMonth           = $this->waimaiEstimateMonth();
        $estimatedMonthlyOrderVolume   = $this->estimatedMonthlyOrderVolume();
        $monthlyProfitEstimate         = $this->actualMonthlyIncome() - (($this->fixedOperatingCostsMonthly()['price'] ?? 0) + ($this->changeOperatingCostsMonthly()['price'] ?? 0));
        $dailyPrincipalProtectionCount = $this->dailyPrincipalProtectionCount();


        return [
            'text'   => '月营收预估',
            'value'  => '',
            'detail' => [
                [
                    '',
                    '保守版',
                    '常规版',
                    '激进版',
                ], [
                    '月实收预估',
                    round($actualMonthlyIncomeValue * (1 - $this->floatingRate)),
                    $actualMonthlyIncomeValue,
                    round($actualMonthlyIncomeValue * (1 + $this->floatingRate)),
                ], [
                    '堂食',
                    round($dineInValue * (1 - $this->floatingRate)),
                    $dineInValue,
                    round($dineInValue * (1 + $this->floatingRate)),
                ], [
                    '外卖',
                    round($waimaiEstimateMonth * (1 - $this->floatingRate)),
                    $waimaiEstimateMonth,
                    round($waimaiEstimateMonth * (1 + $this->floatingRate)),
                ], [
                    '月单量预估',
                    round($estimatedMonthlyOrderVolume * (1 - $this->floatingRate)),
                    $estimatedMonthlyOrderVolume,
                    round($estimatedMonthlyOrderVolume * (1 + $this->floatingRate)),
                ], [
                    '月利润预估',
                    round($monthlyProfitEstimate * (1 - $this->floatingRate)),
                    $monthlyProfitEstimate,  // 月实收 - (月固定支出 + 月变动)
                    round($monthlyProfitEstimate * (1 + $this->floatingRate)),
                ], [
                    '每日保本单量',
                    round($dailyPrincipalProtectionCount * (1 - $this->floatingRate)),
                    $dailyPrincipalProtectionCount,
                    round($dailyPrincipalProtectionCount * (1 + $this->floatingRate)),
                ]
            ]
        ];
    }

    /**
     * 回本周期预估
     */
    public function estimatedPaybackPeriod(): array
    {

        $oneTimeInputCost         = $this->oneTimeInputCost();
        $recoverableCosts         = $this->recoverableCosts()['price'] ?? 0;
        $monthlyOperatingExpenses = $this->monthlyOperatingExpenses();
        $actualMonthlyIncome      = $this->actualMonthlyIncome();
        $paybackCycle             = $this->paybackCycle();
        $totalInvestmentBudget    = (int)$this->totalInvestmentBudget();
        $reliefItems              = $this->reliefItems()['price'] ?? 0;

        return [
            'text'   => '回本周期预估',
            'value'  => '',
            'detail' => [
                [
                    '',
                    '保守版',
                    '常规版',
                    '激进版',
                ], [
                    '一次性投入',
                    $oneTimeInputCost,
                    $oneTimeInputCost,
                    $oneTimeInputCost,
                ], [
                    '总投资预算',
                    $totalInvestmentBudget,
                    $totalInvestmentBudget,
                    $totalInvestmentBudget,
                ], [
                    '可回收金额',
                    $recoverableCosts,
                    $recoverableCosts,
                    $recoverableCosts,
                ], [
                    '月经营支出',
                    round($monthlyOperatingExpenses * (1 + $this->floatingRate)),
                    $monthlyOperatingExpenses,
                    round($monthlyOperatingExpenses * (1 - $this->floatingRate)),
                ], [
                    '月实收预估',
                    round($actualMonthlyIncome * (1 - $this->floatingRate)),
                    $actualMonthlyIncome,
                    round($actualMonthlyIncome * (1 + $this->floatingRate)),
                ], [
                    '减免项目',
                    $reliefItems,
                    $reliefItems,
                    $reliefItems,
                ], [
                    '回本周期',
                    round($paybackCycle * (1 + $this->floatingRate), 2),
                    $paybackCycle,
                    round($paybackCycle * (1 - $this->floatingRate), 2),
                ]
            ]
        ];
    }

    /**
     * 总投资预算
     */
    public function totalInvestmentBudget()
    {
        // 总投资预算= 加盟费 + 设计费 + 软装 + 硬装 + 操作设备 + 电子设备 + 物业费 + 管理费 + 自采设备 + 置物杂物费 + 其他器具等 + 施工围挡 + 商务通 + 开业营销物料 + 开业食材储备 + 培训差旅费 + 前置人工费 + 日用杂费（含水电）
        return $this->franchiseFee() //加盟费
            + $this->managementFee() //管理费
            //+ $this->contractDeposit() //保证金
            + 6000 //设计费
            + 15000 //软装
            + $this->decorationCost() //硬装
            + 18000 //操作设备
            + 20000 //电子设备
            + 1500 //商务通
            + 10000 //自采设备
            + 1500 //置物杂物费
            + 1500 //其他器具等
            + 1500 //开业营销物料
            + 6500 //开业食材储备
            + 3000 //培训差旅费
            + 2000 //前置人工费
            + 3000 //日用杂费
            + (int)$this->siteLocationModel->manageFee
            //+ (int)$this->siteLocationModel->bond
            + (int)$this->siteLocationModel->sponsorshipFee
            + (int)$this->siteLocationModel->promotionFee
            + (int)$this->siteLocationModel->otherFee
            + (int)$this->siteLocationModel->mobilizationFee // 进场费
            + (int)$this->siteLocationModel->propertyFee //物业费
            ;
    }

    /**
     * 一次性投资预算
     */
    public function oneTimeInputCost()
    {
        switch ($this->siteLocationModel->rentType) {
            case SiteLocationEnum::RENT_TYPE_2:
                $initialShopRent = 0;
                break;
            default:
                $initialShopRent = ceil((int)$this->siteLocationModel->fixedRent / 100 / $this->annualPaymentShopCycle); //初期商铺房租
        }

        $totalPrice = $this->franchiseFee() //加盟费
            + $this->managementFee()
            + 6000 //设计费
            + 15000 //软装
            + $this->decorationCost() //硬装
            + 18000 //操作设备
            + 20000 //电子设备
            + (int)$this->siteLocationModel->propertyFee //物业费
            + $initialShopRent
            + ceil((int)$this->siteLocationModel->warehouseFixedRent / $this->annualPaymentWarehouseCycle) //初期库房房租
            + (int)$this->siteLocationModel->mobilizationFee // 进场费
            + 1500 //商务通
            + 10000 //自采设备
            + 1500 //置物杂物费
            + 1500 //其他器具等
            + $this->firstBatchOrderPrice() // 首批订货
            + 1500 //开业营销物料
            + 6500 //开业食材储备
            + 3000 //培训差旅费
            + 2000 //前置人工费
            + 3000 //日常杂费
            + ($this->changeOperatingCostsMonthly()['price'] ?? 0)
            + ($this->recoverableCosts()['price'] ?? 0)
            + (int)$this->siteLocationModel->manageFee
            + (int)$this->siteLocationModel->sponsorshipFee
            + (int)$this->siteLocationModel->promotionFee
            + (int)$this->siteLocationModel->otherFee;

        $firstRent = $this->firstBatchOrderPrice();
        // 订货成本
        $orderCost = ($this->changeOperatingCostsMonthly()['detail'][2]['price'] ?? 0);

        return (int)($totalPrice - min($firstRent, $orderCost));
    }

    /**
     * 月经营支出
     */
    public function monthlyOperatingExpenses(): int
    {
        switch ($this->siteLocationModel->rentType) {
            case SiteLocationEnum::RENT_TYPE_2:
                $initialShopRent = bcmul($this->actualMonthlyIncome(), ((int)$this->siteLocationModel->remind / 100));
                break;
            default:
                $initialShopRent = bcdiv((int)$this->siteLocationModel->fixedRent, 1200);
        }
        return (int)($this->changeOperatingCostsMonthly()['price'] ?? 0) + $initialShopRent + (int)((int)$this->siteLocationModel->warehouseFixedRent / 12);
    }

    /**
     * 回本周期
     */
    public function paybackCycle(): float
    {
        // 回本周期 = (总投资预算 - 补贴) / (月实收 - 月经营支出)
        $meiosis                 = $this->actualMonthlyIncome() - $this->monthlyOperatingExpenses();
        $this->paybackCycleValue = $meiosis ? (($this->totalInvestmentBudget() - $this->reliefItems()['price'] ?? 0) / $meiosis) : 0;

        return round($this->paybackCycleValue, 2);
    }

    /**
     * 日营收
     */
    public function dailyTurnover(): int
    {
        //费用分摊
        $expenseAllocation = (int)(($this->contract ? (
                bcdiv($this->technologyTransferFee(), 36) +
                bcdiv($this->managementFee(), 12) +
                bcdiv($this->initialFee(), 36)
            ) : 0)
            + 167 + 1388 + (int)(((int)$this->siteLocationModel->area * 2500) / 24));

        switch ($this->siteLocationModel->rentType) {
            case SiteLocationEnum::RENT_TYPE_2:
                $rentCost = (int)bcmul($this->actualMonthlyIncome(), ((int)$this->siteLocationModel->remind / 100)) + (int)bcdiv((int)$this->siteLocationModel->warehouseFixedRent, 12);
                break;
            default:
                $rentCost = bcdiv((int)$this->siteLocationModel->fixedRent, 1200) + bcdiv((int)$this->siteLocationModel->warehouseFixedRent, 12);
        }


        // 其他杂费 物业费 + 管理费 + 进场费 + 赞助费 + 推广费 + 其他费用
        $otherCost = bcdiv(
            ((int)$this->siteLocationModel->manageFee) +
            ((int)$this->siteLocationModel->sponsorshipFee) +
            ((int)$this->siteLocationModel->promotionFee) +
            ((int)$this->siteLocationModel->otherFee) +
            ((int)$this->siteLocationModel->propertyFee) +
            ((int)$this->siteLocationModel->mobilizationFee),
            12);

        $rentCost = $rentCost + $otherCost;

        $fixedCostMoney = $this->actualMonthlyIncome() * 0.19 + $rentCost + $expenseAllocation;

        $dailyCost = $fixedCostMoney / 30;

        $foodstuffMarginRate = ((int)$this->siteLocationModel->foodstuffMarginRate ?: 65) / 100;

        return bcdiv($dailyCost, $foodstuffMarginRate);
    }

    /**
     * 每日保本单量
     */
    public function dailyPrincipalProtectionCount(): int
    {

        // 外卖占比
        $waimaiRatio = $this->waimaiEstimateMonth() / $this->actualMonthlyIncome();

        // 日营业额
        $dailyTurnover  = $this->dailyTurnover();
        $waimaiQuantity = $dailyTurnover * $waimaiRatio / 30;

        // 堂食
        $dineInTurnover = $dailyTurnover - $dailyTurnover * $waimaiRatio;
        $dineInQuantity = $this->siteLocationModel->customerUnitPriceEstimation ? ($dineInTurnover / $this->siteLocationModel->customerUnitPriceEstimation) : 0;

        return (int)($waimaiQuantity + $dineInQuantity);
    }

    /**
     * 月预估单量
     */
    public function estimatedMonthlyOrderVolume(): int
    {

        $dineInPrice = ($this->actualMonthlyIncome() - $this->waimaiEstimateMonth()) / ($this->siteLocationModel->foodstuffMarginRate / 100);

        return (int)(($this->siteLocationModel->customerUnitPriceEstimation ? ($dineInPrice / $this->siteLocationModel->customerUnitPriceEstimation) : 0) + ($this->waimaiEstimateMonth() / 30));
    }

}